/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;
import mail.enviarMail;
import negocio.Alquiler;
import negocio.Horario;
import negocio.Horariopista;
import negocio.HorariopistaId;
import negocio.Instalacion;
import negocio.Pista;
import negocio.Usuario;
import negocio.dao.DAO;
import negocio.dao.HorariopistaDAO;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import pdf.generarpdf;

/**
 *
 * @author davidcabrera
 */
public class confirmarAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "confirmar";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        DAO d = new DAO();
        Integer idpista = Integer.valueOf(request.getParameter("idp"));
        Integer idinst = Integer.valueOf(request.getParameter("idi"));
        Integer idhorario = Integer.valueOf(request.getParameter("idh"));
        String precio = request.getParameter("pr");
        String idUsuario = request.getParameter("idu");

        Alquiler alquiler = new Alquiler(idUsuario, idhorario, precio, idpista);

        d.create(alquiler, "Alquiler");


        HorariopistaDAO dh = new HorariopistaDAO();

        Horariopista hp = dh.read(idpista, idhorario);


        hp.setOcupada(Boolean.parseBoolean("true"));
        d.update(hp, "HorarioPista");

        Horario h = (Horario) d.read("Horario", idhorario);

        String ho = h.getHoraInicio().toString().substring(0, 5);
        Usuario u = (Usuario) d.read("Usuario", Integer.valueOf(idUsuario));

        Instalacion ins = (Instalacion) d.read("Instalacion", idinst);
        Pista p = (Pista) d.read("Pista", idpista);

        

        ServletContext sc;
        String ruta;
        sc = getServlet().getServletContext();

        ruta = sc.getRealPath("/pdfs");//Devuelve ruta hasta build/web
        
        Calendar calendario = new GregorianCalendar();
        
        int hora=calendario.get(Calendar.HOUR);
        int min=calendario.get(Calendar.MINUTE);
        int seg=calendario.get(Calendar.SECOND);
        int mil=calendario.get(Calendar.MILLISECOND);
        
        String nom=p.getNombre()+u.getIdUsuario()+"d"+hora+"_"+min+"_"+seg+"_"+mil;
        
        
        ruta+="/"+nom+".PDF";
        
        generarpdf pdf = new generarpdf();
        pdf.generar(ins, p, u, ho,ruta);

        
        enviarMail mail = new enviarMail();

        mail.sendMailReserva(u.getEmail(), u.getNombre(), precio, p.getNombre(), ins.getNombre(), ho, ruta);
        
        


        return mapping.findForward(SUCCESS);
    }
}
